import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';

import { ListResult } from 'app/modules/base/api-base';

import { Repository, RepositoryService } from './repository.service';

@Injectable({
	providedIn: 'root',
})
export class RepositoryListService implements Resolve<Repository> {
	constructor(private service: RepositoryService) {}

	async resolve(route: ActivatedRouteSnapshot) {
		const page = Number(route.queryParamMap.get('page')) || 1;
		const result = await this.service.list({ page: page });
		if (result.list && result.list.length) {
			return result.list[0];
		}
		return null;
	}
}
